//
//  headerView.m
//  HaHa
//
//  Created by lanou3g on 15/9/24.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "headerView.h"

@implementation headerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addControls];
    }
    return self;
}

- (void)addControls
{
    
    
    _HeadPortraitPic = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
    _HeadPortraitPic.layer.cornerRadius = _HeadPortraitPic.width / 2;
    _HeadPortraitPic.layer.masksToBounds = YES;
    [self addSubview:_HeadPortraitPic];
    
    _PublishedName = [[UILabel alloc] initWithFrame:CGRectMake(_HeadPortraitPic.bounds.size.width + 20, 5, kScreenWdth - _HeadPortraitPic.Y_height - 20, 40)];
    [self addSubview:_PublishedName];
    
    _titleName = [[UILabel alloc] initWithFrame:CGRectMake(5, _HeadPortraitPic.Y_height + 10, kScreenWdth - 10, 30)];
    [self addSubview:_titleName];
    
    _image = [[UIImageView alloc] initWithFrame:CGRectMake(5, _titleName.Y_height + 5, kScreenWdth - 10, 200)];
    [self addSubview:_image];
    
   
    
}

- (void)setvAlueWithModel:(FunnyModel *)model
{
    [_HeadPortraitPic sd_setImageWithURL:[NSURL URLWithString:[model.user objectForKey:@"avatar_url"]] placeholderImage:nil];
    _PublishedName.text = [model.user objectForKey:@"name"];
    
    CGFloat textHeight = [self YYHreturnStringHeight:model.text Width:kScreenWdth - 20 Font:17];
    _titleName.frame = CGRectMake(1, _PublishedName.Y_height + 5, kScreenWdth - 20, textHeight);
    
    //图片高度
    NSString *widthTemp = [model.large_image objectForKey:@"width"];
    NSString *imagehieghtTemp = [model.large_image objectForKey:@"height"];
    NSInteger width = [widthTemp integerValue];
    NSInteger height = [imagehieghtTemp integerValue];
    
    //如果大于表视图的宽度 等比缩放到与表视图等宽
    //如果小于表视图宽度 等于表视图宽度
    CGFloat imageWidth;
    CGFloat imageHeight;
    if (width>kScreenWdth-20) {
        CGFloat beishu = width/(kScreenWdth - 20);
        imageHeight = height/beishu;
        imageWidth = kScreenWdth-20;
    }else if(height == 0){
        imageHeight = kScreenWdth-20;
        imageWidth = kScreenWdth-20;
    }else{
        imageHeight = height;
        imageWidth = width;
    }
    _image.frame = CGRectMake((kScreenWdth) / 2 - imageWidth / 2, _titleName.Y_height + 5, imageWidth, imageHeight);
    
    
    _titleName.text = model.text;
    //取出url_list中的网址
    NSArray *urlArr = [model.large_image objectForKey:@"url_list"];
    //将最后一个网址取出来给url
    NSDictionary *url = [urlArr firstObject];
    //用字符串接收
    NSString *URL = [url objectForKey:@"url"];
    [_image sd_setImageWithURL:[NSURL URLWithString:URL relativeToURL:nil]];
}
//计算高度
- (CGFloat)YYHreturnStringHeight:(NSString *)string Width:(CGFloat)Width Font:(int)font{
    
    CGRect frame = [string boundingRectWithSize:CGSizeMake(Width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    return frame.size.height;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
